<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="商品类型管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="类型名称" prop="goodsTypeName">
                <el-input v-model="formData.goodsTypeName"/>
            </el-form-item>
            <el-form-item label="商品性质" prop="goodsNature">
                <el-radio-group v-model="formData.goodsNature">
                    <el-radio :label="1" size="large">实物商品</el-radio>
                    <el-radio :label="2" size="large">虚拟商品</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item>
                <el-popconfirm :title="'确定保存吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {GoodsTypeApi} from '@/api/food/goodsType'
import {Common} from '@/utils/common'

export default {
    name: 'MallGoodsTypeUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: 0,
                goodsTypeName: "",
                goodsNature:1
            },
            rules: {
                goodsTypeName: [
                    {required: true, message: '请输入商品类型名称', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ],
                goodsNature: [
                    {required: true, message: '请选择商品性质', trigger: 'blur'}
                ]
            },
            dialogVisible: false
        }
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            this.dialogVisible = true
            this.$nextTick(() => {
                if (this.$refs['formRef'] !== undefined) {
                    this.$refs['formRef'].resetFields()
                }
                if (data.id > 0) {
                    this.formData = Common.assign(this.formData, data)
                } else {
                    this.formData.id = 0
                }
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.id > 0) {
                        GoodsTypeApi.update(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    } else {
                        GoodsTypeApi.insert(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    }
                }
            })
        }

    }
}
</script>
